using System;
using System.Collections.Generic;
using System.Text;

namespace Placid.Core
{
    public class PSchema
    {
        public enum ElementPosition
        {
            BEFORE,
            AFTER
        }

        public static Dictionary<string, PTreeNodesMeasure.MeasureType> _mtStrings;

        private List<string> _rows;
        private List<string> _columns;

        private Dictionary<string, PTreeNodesMeasure.MeasureType> _ms;

        static PSchema()
        {
            _mtStrings = new Dictionary<string, PTreeNodesMeasure.MeasureType>();
            foreach(int mt in Enum.GetValues(typeof(PTreeNodesMeasure.MeasureType))) {
                _mtStrings.Add(((PTreeNodesMeasure.MeasureType)mt).ToString(), (PTreeNodesMeasure.MeasureType)mt);
            }
        }

        public PSchema()
        {
            _rows = new List<string>();
            _columns = new List<string>();
            _ms = new Dictionary<string, PTreeNodesMeasure.MeasureType>();
        }

        public List<string> Rows
        {
            get { return _rows; }
        }

        public List<string> Columns
        {
            get { return _columns; }
        }

        #region Columns

        public bool AddColumn(string sourceColum, ElementPosition ep, string positionColumn)
        {
            int posIndex;

            DeleteColumn(sourceColum);
            posIndex = _columns.IndexOf(positionColumn);
            if (posIndex > -1)
            {
                switch (ep)
                {
                    case ElementPosition.BEFORE:
                        break;
                    case ElementPosition.AFTER:
                        posIndex += 1;
                        break;
                    default:
                        break;
                }

                _columns.Insert(posIndex, sourceColum);
                return true;
            }
            return false;
        }

        public void AddColumn(string sourceColum)
        {
            DeleteColumn(sourceColum);
            _columns.Add(sourceColum);
        }

        public void DeleteColumn(string sourceColum)
        {
            if (_columns.Contains(sourceColum)) _columns.Remove(sourceColum);
        }

        #endregion

        #region Rows

        public bool AddRow(string sourceColum, ElementPosition ep, string positionColumn)
        {
            int posIndex;

            DeleteRow(sourceColum);
            posIndex = _rows.IndexOf(positionColumn);
            if (posIndex > -1)
            {
                switch (ep)
                {
                    case ElementPosition.BEFORE:
                        break;
                    case ElementPosition.AFTER:
                        posIndex += 1;
                        break;
                    default:
                        break;
                }

                _rows.Insert(posIndex, sourceColum);
                return true;
            }
            return false;
        }

        public void AddRow(string sourceColum)
        {
            DeleteRow(sourceColum);
            _rows.Add(sourceColum);
        }

        public void DeleteRow(string sourceColum)
        {
            if (_rows.Contains(sourceColum)) _rows.Remove(sourceColum);
        }

        #endregion

        #region m

        public void Addm(string sourceColumn, PTreeNodesMeasure.MeasureType type)
        {
            Deletem(sourceColumn);
            _ms.Add(sourceColumn, type);
        }

        public void Addm(string sourceColumn, string type)
        {
            Deletem(sourceColumn);
            if(_mtStrings.ContainsKey(type))
            _ms.Add(sourceColumn, _mtStrings[type]);
        }

        public void Deletem(string sourceColumn)
        {
            if (_ms.ContainsKey(sourceColumn)) _ms.Remove(sourceColumn);
        }

        public Dictionary<string, PTreeNodesMeasure.MeasureType> Ms
        {
            get { return _ms; }
            set { _ms = value; }
        }

        #endregion

        public override string ToString()
        {
            string str = "ROWS: ";
            foreach (string s in _rows)
            {
                str += s + " -> ";
            }
            str += Environment.NewLine + "COLUMNS: ";
            foreach (string s in _columns)
            {
                str += s + " -> ";
            }
            str += Environment.NewLine + "MEASURES: ";
            foreach (string s in _ms.Keys)
            {
                str += _ms[s].ToString() + "(" + s + "), ";
            }

            return str;
        }

    }
}
